<template>
  <aside>
    <el-menu unique-opened :default-active="active" router class="el-menu-vertical-demo" background-color="#373d41" text-color="#E3F8FF" active-text-color="#ffd04b">
      <div v-for="(item,index) in arr">
        <el-menu-item v-if="!item.resources" :index="item.url">
          <i :class="item.icon"></i>
          <span slot="title">{{ item.name }}</span>
        </el-menu-item>

        <el-submenu v-if="item.resources" :index="`${index+1}`">
          <template slot="title">
            <i :class="item.icon"></i>
            <span slot="title">{{ item.name }}</span>
          </template>
          <el-menu-item-group>
            <el-menu-item v-for="(val, key) in item.resources" :index="val.url" :key="key">{{ val.name }}</el-menu-item>
          </el-menu-item-group>
        </el-submenu>
      </div>
    </el-menu>
  </aside>
</template>

<script type="text/ecmascript-6">
  import {post} from '@/utils/http'
  export default {
    data() {
      return {
        arr: []
      }
    },
    methods: {
      getMenu() {
        post('/api/manager/v1/resource/leftMenu').then((response) => {
          this.arr = response.data
        })
      }
    },
    created() {
      this.$store.commit('set_active_index', this.$route.path);
    },
    mounted () {
      this.getMenu();
      // 刷新时以当前路由做为tab加入tabs
      if (this.$route.path !== '/homePage') {
        this.$store.commit('add_tabs', {route: '/homePage', name: '欢迎页'});
        this.$store.commit('add_tabs', {route: this.$route.path , name: this.$route.name });
      } else {
        this.$store.commit('add_tabs', {route: '/homePage', name: '欢迎页'});
      }
      this.$store.commit('set_active_index', this.$route.path);
    },
    computed: {
      options () {
        return this.$store.state.options;
      },
      active() {
        return this.$store.state.activeIndex
      }
    }
  }
</script>

<style lang='scss' scoped>
  aside {
    width: 180px;
    min-width: 50px;
    background: #373d41;
    overflow-y: auto;
    &::-webkit-scrollbar {
      display: none;
    }
    &.showSidebar {
      overflow-x: hidden;
      overflow-y: auto;
    }
    .el-menu {
      width: 180px;
      height: 100%; /*写给不支持calc()的浏览器*/
      height: -moz-calc(100% - 60px);
      height: -webkit-calc(100% - 60px);
      height: calc(100% - 60px);
    }
    .el-menu-vertical-demo:not(.el-menu--collapse) {
      width: 180px;
      min-height: 400px;
    }
  }
</style>
